package com.l024.android_ljl.view.main.activity;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.R;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivityCommentBinding;
import com.l024.android_ljl.entity.SysCommentEntity;
import com.l024.android_ljl.entity.SysOptionEntity;
import com.l024.android_ljl.entity.SysSubjectEntity;
import com.l024.android_ljl.viewmodel.main.SubjectViewModel;
import com.quantstack.llcommonlibrary.adapter.CommonAdapter;
import com.quantstack.llcommonlibrary.adapter.CommonViewHolder;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户评论列表
 */
public class CommentActivity extends BaseActivity<SubjectViewModel, ActivityCommentBinding> implements BaseViewModel.OnPageDataListener {
    private List<SysCommentEntity> list = new ArrayList<>();
    private CommonAdapter<SysCommentEntity> adapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    public void afterCreate() {
        initList();
        mViewModel.setBaseView(this);
        mViewModel.setOnPageDataListener(this);
        mViewModel.getUserComment();
    }

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private void initList() {
       adapter = new CommonAdapter<SysCommentEntity>(list, new CommonAdapter.OnBindDataListener<SysCommentEntity>() {
            @Override
            public void onBindViewHolder(SysCommentEntity model, CommonViewHolder viewHolder, int type, int position) {
                SysSubjectEntity subject = model.getSubject();
                if(subject!=null){
                    viewHolder.setText(R.id.tv_title,subject.getTitle());
                    viewHolder.setText(R.id.tv_answer,"答案:"+subject.getAnswer());
                    viewHolder.setText(R.id.tv_note,"解析:"+subject.getNote());
                    LinearLayout llOptions = viewHolder.getView(R.id.ll_options);
                    List<SysOptionEntity> options = subject.getOptions();
                    if(options!=null&&options.size()>0){
                        llOptions.removeAllViews();
                        for(SysOptionEntity optionEntity:options){
                            TextView view = new TextView(CommentActivity.this);
                            view.setText(optionEntity.getContent());
                            view.setTextSize(18);
                            view.setTextColor(getResources().getColor(R.color.c333));
                            llOptions.addView(view);
                        }
                    }
                }

                //设置评论
                viewHolder.setImageUrl(CommentActivity.this,R.id.iv_avator,model.getUser().getAvator());
                viewHolder.setText(R.id.tv_username,model.getUser().getName());
                viewHolder.setText(R.id.tv_comment,model.getComment());
                String sd = sdf.format(new Date(model.getCommentTime())); // 时间戳转换日期
                viewHolder.setText(R.id.tv_time,sd);
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.item_user_comment;
            }
        });
        mViewDataBind.rlComments.setLayoutManager(new LinearLayoutManager(this));
        mViewDataBind.rlComments.setAdapter(adapter);
    }

    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.USER_COMMENTS:{
                list.clear();
                List<SysCommentEntity> value = mViewModel.getUserComments().getValue();
                list.addAll(value);
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.USER_COMMENTS:{
                ToastUtils.show(msg);
                break;
            }
        }
    }
}